﻿using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Read.API.application.Command.Permission;
using BasicData.Read.API.application.Command.Role;
using BasicData.Read.API.application.Command.User;
using BasicData.Read.API.DTO;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BasicData.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IMediator mediator;

        public UserController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回</returns>
        [HttpGet]
        public Task<ApiResult<UserDTO>> Login([FromQuery]UserCommand request)
        {
            try
            {
                return mediator.Send(request);

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回</returns>
        [HttpGet]
        public Task<ApiResult<List<UserModel>>> UserList([FromQuery]UserQueryCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回</returns>
        [HttpGet]
        public Task<ApiResult<List<RoleModel>>> RoleList([FromQuery] RoleQueryCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

      /// <summary>
      /// 根据用户信息反填角色
      /// </summary>
      /// <param name="request">命令</param>
      /// <returns>返回</returns>

      [HttpGet]
        public Task<ApiResult<List<int>>> FindRoleList([FromQuery] FindRoleCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询权限树状
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回</returns>
        [HttpGet]
        public Task<ApiResult<List<PermissionDTO>>> GetPermissionList([FromQuery]PermissionQueryTreeCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回</returns>
        [HttpGet]
        public Task<ApiResult<List<PermissionModel>>> Getpermission(GetPermissioListCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据角色反填权限
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回</returns>
        [HttpGet]
        public Task<ApiResult<List<int>>> FindRPlist([FromQuery] FindPermissionCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
       /// <summary>
       /// 根据树形反填权限Id
       /// </summary>
       /// <param name="request">命令</param>
       /// <returns>返回</returns>
       [HttpGet]
        public Task<ApiResult<PermissionModel>> FindPermission([FromQuery]FindPermissionTreeCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }






    }
}
